class ErrorHandler:
    _instance = None
    _error_callback = None

    def __new__(cls):
        # 单例模式
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._init_defaults()
        return cls._instance

    def _init_defaults(self):
        # 默认使用print输出错误
        self._error_callback = lambda msg: print(f"ERROR: {msg}")

    def register_callback(self, callback):
        """供前端框架注册错误回调"""
        self._error_callback = callback

    def handle_error(self, message, exc=None):
        """统一错误处理入口"""
        if exc:
            message += f"\nException: {str(exc)}"
        self._error_callback(message)